package cn.tedu.xmfhlittlebird.service.impl;

import cn.tedu.xmfhlittlebird.ex.ServiceCode;
import cn.tedu.xmfhlittlebird.ex.ServiceException;
import cn.tedu.xmfhlittlebird.mapper.CouponIssueMapper;
import cn.tedu.xmfhlittlebird.pojo.dto.CouponIssueAddNewDTO;
import cn.tedu.xmfhlittlebird.pojo.dto.CouponIssueUpdateDTO;
import cn.tedu.xmfhlittlebird.pojo.entity.StoreCoupon;
import cn.tedu.xmfhlittlebird.pojo.entity.StoreCouponIssue;
import cn.tedu.xmfhlittlebird.pojo.vo.StoreCouponIssueListItemVO;
import cn.tedu.xmfhlittlebird.pojo.vo.StoreCouponIssueStandardVO;
import cn.tedu.xmfhlittlebird.pojo.vo.StoreCouponStandardVO;
import cn.tedu.xmfhlittlebird.service.ICouponIssueService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class CouponIssueServiceImpl implements ICouponIssueService {
    @Autowired
    private CouponIssueMapper couponIssueMapper;

    @Override
    public void addNew(CouponIssueAddNewDTO couponIssueAddNewDTO) {
        log.debug("开始处理【添加优惠券领取信息】的业务");
        // 创建优惠券领取信息对象，用于插入到数据表
        StoreCouponIssue storeCouponIssue = new StoreCouponIssue();
        BeanUtils.copyProperties(couponIssueAddNewDTO, storeCouponIssue);

        // 插入数据
        log.debug("即将向数据库中插入数据：{}", storeCouponIssue);
        int rows = couponIssueMapper.insert(storeCouponIssue);
        if (rows != 1) {
            String message = "添加优惠券领取信息失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }

    @Override
    public void deleteById(Long id) {
        log.debug("开始处理【删除优惠券领取信息】的业务，参数：{}", id);
        // 调用Mapper对象的getStandardById()方法执行查询
        StoreCouponIssueStandardVO queryResult = couponIssueMapper.getStandardById(id);
        // 判断查询结果是否为null
        if (queryResult == null) {
            // 是：此id对应的数据不存在，则抛出异常(ERR_NOT_FOUND)
            String message = "删除优惠券领取信息失败，尝试删除的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        // 调用Mapper对象的deleteById()执行删除，并获取返回值
        int rows = couponIssueMapper.deleteById(id);
        // 判断以上返回值是否不为1
        if (rows != 1) {
            // 是：抛出异常(ERR_DELETE)
            String message = "删除优惠券领取信息失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
    }

    @Override
    public void updateById(Long id, CouponIssueUpdateDTO couponIssueUpdateDTO) {
        log.debug("开始处理【修改优惠券领取信息】的业务，参数：{}", id);
        // 调用Mapper对象的getStandardById()方法执行查询
        StoreCouponIssueStandardVO queryResult = couponIssueMapper.getStandardById(id);
        // 判断查询结果是否为null
        if (queryResult == null) {
            // 是：此id对应的数据不存在，则抛出异常(ERR_NOT_FOUND)
            String message = "修改优惠券领取信息失败，尝试修改的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        StoreCouponIssue storeCouponIssue = new StoreCouponIssue();
        BeanUtils.copyProperties(couponIssueUpdateDTO, storeCouponIssue);
        storeCouponIssue.setId(id);

        // 修改数据
        log.debug("即将修改数据：{}", storeCouponIssue);
        int rows = couponIssueMapper.updateById(storeCouponIssue);
        if (rows != 1) {
            String message = "修改优惠券领取信息失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }

    }

    @Override
    public void setPermanent(Long id) {
        log.debug("开始处理【设置无限优惠券】的业务，参数：{}", id);
        // 调用Mapper对象的getDetailsById()方法执行查询
        StoreCouponIssueStandardVO queryResult = couponIssueMapper.getStandardById(id);
        // 判断查询结果是否为null
        if (queryResult == null) {
            // 是：此id对应的数据不存在，则抛出异常(ERR_NOT_FOUND)
            String message = "设置无限优惠券，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        // 判断查询结果中的Status是否为1
        if (queryResult.getIsPermanent() == 1) {
            // 是：此id对应的优惠券已经是无限张数，则抛出异常(ERR_CONFLICT)
            String message = "设置无限优惠券失败，当前优惠券已经处于无限张数！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        StoreCouponIssue storeCouponIssue = new StoreCouponIssue();
        storeCouponIssue.setId(id);
        storeCouponIssue.setStatus(1);
        storeCouponIssue.setRemainCount(2147483647);

        // 修改数据
        log.debug("即将修改数据：{}", storeCouponIssue);
        int rows = couponIssueMapper.updateById(storeCouponIssue);
        if (rows != 1) {
            String message = "修改优惠券领取信息失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }

    @Override
    public void setTemporary(Long id) {
        log.debug("开始处理【取消无限优惠券】的业务，参数：{}", id);
        // 调用Mapper对象的getDetailsById()方法执行查询
        StoreCouponIssueStandardVO queryResult = couponIssueMapper.getStandardById(id);
        // 判断查询结果是否为null
        if (queryResult == null) {
            // 是：此id对应的数据不存在，则抛出异常(ERR_NOT_FOUND)
            String message = "取消无限优惠券，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        // 判断查询结果中的Status是否为0
        if (queryResult.getIsPermanent() == 0 ){
            // 是：此id对应的优惠券已经是有限张数，则抛出异常(ERR_CONFLICT)
            String message = "取消无限优惠券失败，当前优惠券已经处于有限张数！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        StoreCouponIssue storeCouponIssue = new StoreCouponIssue();
        storeCouponIssue.setId(id);
        storeCouponIssue.setStatus(0);
        storeCouponIssue.setRemainCount(200);

        // 修改数据
        log.debug("即将修改数据：{}", storeCouponIssue);
        int rows = couponIssueMapper.updateById(storeCouponIssue);
        if (rows != 1) {
            String message = "修改优惠券领取信息失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }

    @Override
    public StoreCouponIssueStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据id查询优惠券领取详情】的业务");
        StoreCouponIssueStandardVO couponStandardVO = couponIssueMapper.getStandardById(id);
        if (couponStandardVO == null) {
            // 是：此id对应的数据不存在，则抛出异常(ERR_NOT_FOUND)
            String message = "获取优惠券领取详情失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        return couponStandardVO;
    }

    @Override
    public List<StoreCouponIssueListItemVO> list() {
        log.debug("开始处理【查询优惠券领取列表】的业务");
        return couponIssueMapper.list();
    }
}
